package com.android.JiYclocks;

import com.android.JiYclock.DatabaseHelper;
import com.android.JiYclock.Log;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class SpecialAlarmProvider extends ContentProvider {
	
    private SQLiteOpenHelper mOpenHelper;

    private static final int SPECIALALARMS = 1;
    private static final int SPECIALALARMS_ID = 2;
    
    private static final UriMatcher sURLMatcher = new UriMatcher(
            UriMatcher.NO_MATCH);

    static {
        sURLMatcher.addURI("com.android.superdeskclock.provider.specialalarm", "specialalarms", SPECIALALARMS);
        sURLMatcher.addURI("com.android.superdeskclock.provider.specialalarm", "specialalarms/#", SPECIALALARMS_ID);
    }
    public SpecialAlarmProvider() {
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri url, String[] projectionIn, String selection,
            String[] selectionArgs, String sort) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        // Generate the body of the query
        int match = sURLMatcher.match(url);
        switch (match) {
            case SPECIALALARMS:
                qb.setTables("SPECIALALARMS");
                break;
            case SPECIALALARMS_ID:
                qb.setTables("SPECIALALARMS");
                qb.appendWhere("_id=");
                qb.appendWhere(url.getPathSegments().get(1));
                break;
            default:
                throw new IllegalArgumentException("Unknown URL " + url);
        }

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
                              null, null, sort);

        if (ret == null) {
           Log.v("SPECIALALARMS.query: failed");
        } else {
            ret.setNotificationUri(getContext().getContentResolver(), url);
        }

        return ret;
    }

    @Override
    public String getType(Uri url) {
        int match = sURLMatcher.match(url);
        switch (match) {
            case SPECIALALARMS:
                return "vnd.android.cursor.dir/SPECIALALARMS";
            case SPECIALALARMS_ID:
                return "vnd.android.cursor.item/SPECIALALARMS";
            default:
                throw new IllegalArgumentException("Unknown URL");
        }
    }

    @Override
    public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
        int count;
        long rowId = 0;
        int match = sURLMatcher.match(url);
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        switch (match) {
            case SPECIALALARMS_ID: {
                String segment = url.getPathSegments().get(1);
                rowId = Long.parseLong(segment);
                count = db.update("SPECIALALARMS", values, "_id=" + rowId, null);
                break;
            }
            default: {
                throw new UnsupportedOperationException(
                        "Cannot update URL: " + url);
            }
        }
        Log.v("*** notifyChange() rowId: " + rowId + " url " + url);
        getContext().getContentResolver().notifyChange(url, null);
        return count;
    }

    @Override
    public Uri insert(Uri url, ContentValues initialValues) {
        if (sURLMatcher.match(url) != SPECIALALARMS) {
            throw new IllegalArgumentException("Cannot insert into URL: " + url);
        }

        ContentValues values = new ContentValues(initialValues);

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert("SPECIALALARMS", SpecialAlarm.Columns.MESSAGE, values);
        if (rowId < 0) {
            throw new SQLException("Failed to insert row into " + url);
        }
       Log.v("Added alarm rowId = " + rowId);

        Uri newUrl = ContentUris.withAppendedId(SpecialAlarm.Columns.CONTENT_URI, rowId);
        getContext().getContentResolver().notifyChange(newUrl, null);
        return newUrl;
    }

    public int delete(Uri url, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        Log.v("SpecialAlarmProvider...是否执行");
        int count;
        long rowId = 0;
        switch (sURLMatcher.match(url)) {
            case SPECIALALARMS:
            	   Log.v("SpecialAlarmProvider...是否执行--------SPECIALALARMS-");
                count = db.delete("SPECIALALARMS", where, whereArgs);
                break;
            case SPECIALALARMS_ID:
                String segment = url.getPathSegments().get(1);
                rowId = Long.parseLong(segment);
                if (TextUtils.isEmpty(where)) {
                    where = "_id=" + segment;
                } else {
                    where = "_id=" + segment + " AND (" + where + ")";
                }
                Log.v("SpecialAlarmProvider...是否执行--------SPECIALALARMS++++");
                count = db.delete("SPECIALALARMS", where, whereArgs);
                break;
            default:
                throw new IllegalArgumentException("Cannot delete from URL: " + url);
        }

        getContext().getContentResolver().notifyChange(url, null);
        return count;
    }
}
